package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.ITempMsgDao;
import com.qymerge.manage.base.dto.TempMsgDto;
import com.qymerge.manage.base.ro.TempMsgRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.util.*;

/**
 *<p>Title: TempMsgDaoImpl</p>
 *<p>Description: H5预约</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19
 *@Time 17:55
 */
@Repository
public class TempMsgDaoImpl extends BaseDao implements ITempMsgDao {

    /**
     * 查询所有分页
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public List list(TempMsgDto ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = ParamKey.NameSpace.TableSelect.tempmsg;
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getTempmsgid() > 0) {
            sql += " and tempmsgid=:tempmsgid";
            paramMap.put("tempmsgid", ro.getTempmsgid());
        }

        if (StringUtils.isNotBlank(ro.getUsername())) {
            sql += " and username like :username";
            paramMap.put("username", "%" + ro.getUsername() + "%");
        }

        if (StringUtils.isNotBlank(ro.getMobile())) {
            sql += " and mobile like :mobile";
            paramMap.put("mobile", "%" + ro.getMobile() + "%");
        }

        if (StringUtils.isNotBlank(ro.getHxcode())) {
            sql += " and hxcode like :hxcode";
            paramMap.put("hxcode", "%" + ro.getHxcode() + "%");
        }

        if (ro.getIsuse() > -1) {
            sql += " and isuse=:isuse";
            paramMap.put("isuse", ro.getIsuse());
        }

        if (StringUtils.isNotBlank(ro.getHx_time_begin()) && StringUtils.isNotBlank(ro.getHx_time_end())) {
            sql += " and lastlogin BETWEEN :last_time_begin AND :last_time_end";
            paramMap.put("last_time_begin", ro.getHx_time_begin());
            paramMap.put("last_time_end", ro.getHx_time_end());
        }

        if (StringUtils.isNotBlank(ro.getCreate_time_begin()) && StringUtils.isNotBlank(ro.getCreate_time_end())) {
            sql += " and creaetdate BETWEEN :create_time_begin AND :create_time_end";
            paramMap.put("create_time_begin", ro.getCreate_time_begin());
            paramMap.put("create_time_end", ro.getCreate_time_end());
        }

        // 分页数据获取
        sql += " order by tempmsgid desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return jdbc.query(sql, paramMap, new BeanPropertyRowMapper<TempMsgRO>(TempMsgRO.class));
    }

    /**
     * 所有数量
     *
     * @param
     * @return
     */
    @Override
    public int total(TempMsgDto ro) {
        String sql = "select count(1) from tempmsg where 1=1";
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getTempmsgid() > 0) {
            sql += " and tempmsgid=:tempmsgid";
            paramMap.put("tempmsgid", ro.getTempmsgid());
        }

        if (StringUtils.isNotBlank(ro.getUsername())) {
            sql += " and username like :username";
            paramMap.put("username", "%" + ro.getUsername() + "%");
        }

        if (StringUtils.isNotBlank(ro.getMobile())) {
            sql += " and mobile like :mobile";
            paramMap.put("mobile", "%" + ro.getMobile() + "%");
        }

        if (StringUtils.isNotBlank(ro.getHxcode())) {
            sql += " and hxcode like :hxcode";
            paramMap.put("hxcode", "%" + ro.getHxcode() + "%");
        }

        if (ro.getIsuse() > -1) {
            sql += " and isuse=:isuse";
            paramMap.put("isuse", ro.getIsuse());
        }

        if (StringUtils.isNotBlank(ro.getHx_time_begin()) && StringUtils.isNotBlank(ro.getHx_time_end())) {
            sql += " and lastlogin BETWEEN :last_time_begin AND :last_time_end";
            paramMap.put("last_time_begin", ro.getHx_time_begin());
            paramMap.put("last_time_end", ro.getHx_time_end());
        }

        if (StringUtils.isNotBlank(ro.getCreate_time_begin()) && StringUtils.isNotBlank(ro.getCreate_time_end())) {
            sql += " and creaetdate BETWEEN :create_time_begin AND :create_time_end";
            paramMap.put("create_time_begin", ro.getCreate_time_begin());
            paramMap.put("create_time_end", ro.getCreate_time_end());
        }

        return jdbc.queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public TempMsgRO findOne(Long id) {
        StringBuffer sql = new StringBuffer(ParamKey.NameSpace.TableSelect.tempmsg);
        sql.append("and tempmsgid=?");
        RowMapper<TempMsgRO> rowMapper = new BeanPropertyRowMapper<TempMsgRO>(TempMsgRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<TempMsgRO> findAll(TempMsgDto ro) {
        StringBuffer sql = new StringBuffer(ParamKey.NameSpace.TableSelect.tempmsg);
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getTempmsgid() > 0) {
            sql.append(" and tempmsgid=:tempmsgid");
            paramMap.put("tempmsgid", ro.getTempmsgid());
        }

        if (StringUtils.isNotBlank(ro.getUsername())) {
            sql.append(" and username like :username");
            paramMap.put("username", "%" + ro.getUsername() + "%");
        }

        if (StringUtils.isNotBlank(ro.getMobile())) {
            sql.append( " and mobile like :mobile");
            paramMap.put("mobile", "%" + ro.getMobile() + "%");
        }

        if (StringUtils.isNotBlank(ro.getHxcode())) {
            sql.append(" and hxcode like :hxcode");
            paramMap.put("hxcode", "%" + ro.getHxcode() + "%");
        }

        if (ro.getIsuse() > -1) {
            sql.append(" and isuse=:isuse");
            paramMap.put("isuse", ro.getIsuse());
        }

        if (StringUtils.isNotBlank(ro.getHx_time_begin()) && StringUtils.isNotBlank(ro.getHx_time_end())) {
            sql.append(" and lastlogin BETWEEN :last_time_begin AND :last_time_end");
            paramMap.put("last_time_begin", ro.getHx_time_begin());
            paramMap.put("last_time_end", ro.getHx_time_end());
        }

        if (StringUtils.isNotBlank(ro.getCreate_time_begin()) && StringUtils.isNotBlank(ro.getCreate_time_end())) {
            sql.append(" and creaetdate BETWEEN :create_time_begin AND :create_time_end");
            paramMap.put("create_time_begin", ro.getCreate_time_begin());
            paramMap.put("create_time_end", ro.getCreate_time_end());
        }

        sql.append(" order by tempmsgid desc");
        return jdbc.query(sql.toString(), paramMap, new BeanPropertyRowMapper<TempMsgRO>(TempMsgRO.class));
    }

    @Override
    public int install(TempMsgRO ro) {
        StringBuilder sql = new StringBuilder("insert into card(username,mobile,hxcode,isuse,hxdate,createdate) values(?,?,?,?,?,?)");

        List params = new ArrayList();
        params.add(ro.getUsername());
        params.add(ro.getMobile());
        params.add(ro.getHxcode());
        params.add(ro.getIsuse());
        params.add(ro.getHxdate());
        params.add(ro.getCreatedate());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int update(TempMsgRO ro) {
        StringBuilder sql = new StringBuilder("update tempmsg set username=?,mobile=?,hxcode=?,isuse=?,hxdate=?,createdate=? ");

        List params = new LinkedList();
        params.add(ro.getUsername());
        params.add(ro.getMobile());
        params.add(ro.getHxcode());
        params.add(ro.getIsuse());
        params.add(ro.getHxdate());
        params.add(ro.getCreatedate());

        sql.append("where tempmsgid=?");
        params.add(ro.getTempmsgid());
        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int remove(List<Long> ids) {
        // 删除
        String sql="delete from tempmsg where tempmsgid=?";
        final List<Long> idss = ids;
        BatchPreparedStatementSetter setterMid = new BatchPreparedStatementSetter(){

            @Override
            public int getBatchSize(){
                return idss.size();
            }

            @Override
            public void setValues(PreparedStatement ps, int i){
                Long id = idss.get(i);
                try {
                    ps.setLong(1, id);
                } catch(Exception e){
                    logger.error("batchInsertWaitAward Exception:", e);
                }
            }
        };
        return getJdbcTemplate().batchUpdate(sql, setterMid).length;
    }

    @Override
    public int updateIsuse(int isuse) {
        StringBuilder builder = new StringBuilder("update tempmsg set isuse=? ");
        builder.append("where tempmsgid=?");
        return getJdbcTemplate().update(builder.toString(), isuse);
    }

}
